package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.MemberDiscount;

import dao.DaoHelper;
import dao.DiscountDao;

public class DiscountDaoImp implements DiscountDao {

	private static DiscountDaoImp discountDao = new DiscountDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private DiscountDaoImp(){}

	public static DiscountDaoImp getInstance()
	{
		return discountDao;
	}

	@Override
	public boolean save(MemberDiscount memberDiscount) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("insert into memberdiscount value(?, ?)");
			stmt.setFloat(1, memberDiscount.getConsumption());
			stmt.setFloat(2, memberDiscount.getDiscount());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;

	}

	@Override
	public boolean updateByConsumption(MemberDiscount memberDiscount) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("update memberdiscount set discount = ? where consumption = ?");
			stmt.setFloat(1, memberDiscount.getDiscount());
			stmt.setFloat(2, memberDiscount.getConsumption());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;

	}

	@Override
	public List<MemberDiscount> findAll() {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		float c = 0;
		float d = 0;
		MemberDiscount memberDiscount = null;
		ArrayList<MemberDiscount> list = new ArrayList<MemberDiscount>();

		try {	
			stmt=con.prepareStatement("select * from memberdiscount");
			result=stmt.executeQuery();
			while (result.next()) {
				c = result.getFloat(1);
				d = result.getFloat(2);
				memberDiscount = new MemberDiscount(c, d);
				list.add(memberDiscount);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return list;

	}

	@Override
	public MemberDiscount find(float consumption) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		float c = 0;
		float d = 0;
		MemberDiscount memberDiscount = null;

		try {	
			stmt=con.prepareStatement("select * from memberdiscount where consumption = ?");
			stmt.setFloat(1, consumption);
			result=stmt.executeQuery();
			if (result.next()) {
				c = result.getFloat(1);
				d = result.getFloat(2);
				memberDiscount = new MemberDiscount(c, d);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return memberDiscount;

	}

	@Override
	public MemberDiscount findHighest(float consumption) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		float c = 0;
		float d = 0;
		MemberDiscount memberDiscount = null;

		try {	
			stmt=con.prepareStatement("select * from memberdiscount where consumption <= ? order by consumption desc");
			stmt.setFloat(1, consumption);
			result=stmt.executeQuery();
			if (result.next()) {
				c = result.getFloat(1);
				d = result.getFloat(2);
				memberDiscount = new MemberDiscount(c, d);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return memberDiscount;

	}

	@Override
	public boolean removeByConsumption(float consumption) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("delete from memberdiscount where consumption = ?");
			stmt.setFloat(1, consumption);
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;

	}

}
